package org.itboys.core.controller.front.invite;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.core.service.mongo.front.SundayFrontMemberService;
import org.itboys.core.util.AccountIdHoldUtils;
import org.itboys.core.util.InviteCodeUtil;
import org.itboys.core.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author：贾琦磊
 * @Date：2018/12/10
 * @Description：邀请好友
 */
@Controller
@RequestMapping("/sunday/mobile/invite")
@Api(value = "invite", description = "邀请")
public class SundayInviteController {
    @Autowired
    private SundayFrontMemberService memberService;

    @RequestMapping(value = "/checkInviteCode", method = RequestMethod.POST)
    @ApiOperation("填写邀请码")
    public void checkInviteCode(HttpServletRequest request, HttpServletResponse response
            , @ApiParam(value = "邀请码", required = true) @RequestParam(value = "inviteCode") String inviteCode
    ) {
        Long memberId = AccountIdHoldUtils.getMemberId();
        memberService.invite(memberId, inviteCode);
        ResponseMessage.success();
    }

    @RequestMapping(value = "/getInviteCode", method = RequestMethod.POST)
    @ApiOperation("获取邀请码")
    public void getInviteCode(HttpServletRequest request, HttpServletResponse response
    ) {
        Long memberId = AccountIdHoldUtils.getMemberId();
        String code = InviteCodeUtil.idToCode(memberId);
        Map<String, Object> result = new HashMap<>();
        result.put("code", code);
        ResponseMessage.success(result);
    }
}
